<?php
	header("Content-type: text/plain");
	include "db.inc.php";
	
	$clean = array();
	$clean["id"] = preg_replace("/\D/","",$_GET["id"]);
	
	$sql = mysql_query("SELECT * FROM m_movies WHERE imdbID=".$clean["id"]." LIMIT 1");
	
	if(mysql_num_rows($sql) > 0){
		$movie = mysql_fetch_array($sql, MYSQL_ASSOC);
		$clean["id"] = $movie["mID"];
	}else{
		echo "Movie not found"; exit;
	}
?>
<?php
	$sql = mysql_query("SELECT * FROM m_certificate WHERE movID=".$clean["id"]);
	$certs = array();
	if(mysql_num_rows($sql) > 0){
		while($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
			$certs[$row["country"]] = $row["certificate"];
		}
	}
	if(isset($certs["Belgium"])){ echo '	<rating>'.$certs["Belgium"].' (Belgium)</rating>'."\n"; }
	elseif(isset($certs["USA"])){ echo '	<rating>'.$certs["USA"].'</rating>'."\n"; }
?>
	<genres>
<?php
	$temp = array();
	$sql = mysql_query("SELECT * FROM m_genre WHERE movID=".$clean["id"]." ORDER BY mainGenre DESC, genreName");
	if(mysql_num_rows($sql) > 0){
		$i=0;
		while($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
			echo '		<genre'.(($i==0)?' primary="true"':'').'>'.$row["genreName"].'</genre>'."\n";
			$i++;
		}
	}
?>
	</genres>
<?php
	$oldtype = "";
	$sql = mysql_query("SELECT * FROM m_cast WHERE movID=".$clean["id"]." ORDER BY cType, cOrdernr");
	if(mysql_num_rows($sql) > 0){
		while($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
			if($row["cName"] == ""){ continue; }
			if($oldtype != $row["cType"]){
				if($oldtype != ""){
					switch($oldtype){
						case "actor": echo '	</cast>'."\n"; break;
						case "producer": echo '	</producers>'."\n"; break;
						case "director": echo '	</directors>'."\n"; break;
						case "author": echo '	</authors>'."\n"; break;
					}
				}
				switch($row["cType"]){
					case "actor": echo '	<cast>'."\n"; break;
					case "producer": echo '	<producers>'."\n"; break;
					case "director": echo '	<directors>'."\n"; break;
					case "author": echo '	<authors>'."\n"; break;
				}
				$oldtype = $row["cType"];
			}
			echo '		<name>'.utf8_decode($row["cName"]).'</name>'."\n";
		}
	}
	if($oldtype != $row["cType"]){
		if($oldtype != ""){
			switch($oldtype){
				case "actor": echo '	</cast>'."\n"; break;
				case "producer": echo '	</producers>'."\n"; break;
				case "director": echo '	</directors>'."\n"; break;
				case "author": echo '	</authors>'."\n"; break;
			}
		}
	}
?>